<template>
  <el-card>
    <el-row justify="end">
      <el-button type="primary" @click="isShowAdd = true">添加班级</el-button>
    </el-row>
  </el-card>

  <el-table :data="classList">
    <el-table-column prop="name"></el-table-column>
  </el-table>

  <el-dialog v-model="isShowAdd" title="添加学生" @close="clearForm">
    <el-form>
      <el-form-item label="名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitAdd" type="primary">提交</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { getClasses, addClass } from '@/api/classes'
import { onMounted, reactive, ref } from 'vue'

const classList = ref([])
const isShowAdd = ref(false)
const formData = reactive({
  name: ''
})

const loadPage = async() => {
  const res = await getClasses()
  classList.value = res
}
const clearForm = () => {
  formData.name = ''
}
const submitAdd = async() => {
  await addClass(formData)
  loadPage()
  isShowAdd.value = false
}
onMounted(async() => {
  loadPage()
})
</script>
